package com.atguigu.guli.service.cms.controller.api;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.cms.entity.Ad;
import com.atguigu.guli.service.cms.service.AdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@CrossOrigin //解决跨域问题
@Api(tags = "广告推荐")
@RestController
@RequestMapping("/api/cms/ad")
public class ApiAdController {


    @Autowired
    private AdService adService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("根据推荐位id显示广告推荐")
    @GetMapping("list/{adTypeId}")
    public R listByTypeId(@ApiParam (value = "推荐位", required = true)
                          @PathVariable String adTypeId) {
        List<Ad> ads = adService.selectByAdTypeId(adTypeId);
        return R.ok().data("items",ads);
    }


    @PostMapping("save-test")
    public R saveAd(){
        Ad ad = new Ad();
        ad.setTitle("小邓按摩");
        ad.setColor("blue");
        redisTemplate.opsForValue().set("ad-test",ad);
        return R.ok();
    }

    //信息的分类存储1
    @PostMapping("save-code")
    public R saveCode(){
        redisTemplate.opsForValue().set("code:18727892835", "1234"); //设置过期时间
        redisTemplate.opsForValue().set("code:18727892836", "1235");

        redisTemplate.opsForValue().set("info:13766816630", "小邓");
        redisTemplate.opsForValue().set("info:13766816631", "annie");
        return R.ok();
    }

    //信息的分类存储1
    @GetMapping("get-code")
    public R getCode(){

        //判断值在不在
        Boolean result = redisTemplate.hasKey("code:18727892836");
        String code2 = "no value";
        if (result) {

             code2 = (String) redisTemplate.opsForValue().get("code:18727892836");
        }
        String code1 = (String) redisTemplate.opsForValue().get("code:18727892835");
        return R.ok().data("code1",code1).data("code2",code2);
    }
    @DeleteMapping("remove-test")
    public R removeAd(){
        redisTemplate.delete("code:18727892835"); //删除
        return R.ok();
    }

}
